# Monarc makefile

CC  = gcc
AS  = nasm
LD  = ld
AR  = ar
SED = sed
PL = perl

ifndef BASE
	BASE = ..
endif

INCL = \
	-I$(BASE)/lib/libc/include \
	-I$(BASE)/lib/libsys/include \
	-I$(BASE)/lib/libsupc++/include \
	-I$(BASE)/lib/agg/include \
	-I$(BASE)/lib/gui/include \
	-I$(BASE)/../kernel/modules \
	-Iinclude \

LIBSUPCPP_PATH = $(BASE)/lib/libsupc++
LIBSYS_PATH = $(BASE)/lib/libsys

SERVICES_PATH = $(BASE)
	
LIB_PATH = \
	-L$(BASE)/lib/libc \
	-L$(LIBSUPCPP_PATH) \
	-L$(LIBSYS_PATH) \
	-L$(BASE)/lib/agg \
	-L$(BASE)/lib/gui \
	
LIBS = \
	-llibc \
	-lsys \
	-lsupc++ \
	-lagg \
	-lgui \

LIB_DEPS = \
	$(LIBSUPCPP_PATH)/libsupc++.a \
	$(LIBSYS_PATH)/libsys.a \
	$(BASE)/lib/libc/liblibc.a \
	$(BASE)/lib/agg/libagg.a \
	$(BASE)/lib/gui/libgui.a \

DEFINES = \
#	-D__DEBUG \

OPT_OR_DEBUG = \
	-O

C_OPT = \
	-Wall \
	$(OPT_OR_DEBUG) \
	-mno-stack-arg-probe \
	-ffreestanding \
	-nostartfiles \
	-nodefaultlibs \
	-nostdlib \
	-nostdinc \
	-T $(SERVICES_PATH)/service.ld \
	$(INCL) \
	$(DEFINES)

OC_OPT = \
	-Wall \
	$(OPT_OR_DEBUG) \
	-mno-stack-arg-probe \
	-ffreestanding \
	-nostartfiles \
	-nodefaultlibs \
	-nostdlib \
	-nostdinc \
	$(INCL) \
	$(DEFINES)

CC_OPT = \
	-Wall \
	$(OPT_OR_DEBUG) \
	-mno-stack-arg-probe \
	-nostartfiles \
	-fno-builtin \
	-nodefaultlibs \
	-nostdlib \
	-nostdinc \
	-T $(SERVICES_PATH)/service.ld \
	$(INCL) \
	$(DEFINES)
	
OCC_OPT = \
	-Wall \
	$(OPT_OR_DEBUG) \
	-mno-stack-arg-probe \
	-nostartfiles \
	-fno-builtin \
	-nodefaultlibs \
	-nostdlib \
	-nostdinc \
	$(INCL) \
	$(DEFINES)

MKDIR = mkdir
CP = cp
CAT = cat
RM = rm -f

SERVICE_MODULES = \
	Server.o \
	
LARGE_DATA = \
	resources/pointer.tga.h \

all: service.lzss

clean:
	$(RM) service.exe service.lzss *.o service.map

service.lzss: service.exe
	$(BASE)/lzss e service.exe service.lzss
	$(SERVICES_PATH)/bin2c service.lzss

service.exe : main.cc $(SERVICE_MODULES) $(LARGE_DATA) $(LIB_DEPS)
	$(CC) $(CC_OPT) main.cc $(SERVICE_MODULES) $(LARGE_DATA) $(LIB_PATH) -Wl,--start-group $(LIBS) -Wl,--end-group -Wl,-Map,service.map -o service.exe
	objdump -t service.exe | perl $(SERVICES_PATH)/symbols.pl > service.sym

resources/pointer.tga.h: resources/pointer.tga
	$(SERVICES_PATH)/bin2c $<

%.o : %.asm
	$(AS) -fwin32 $< -o $@

%.o: %.c %.h
	$(CC) $(OC_OPT) -c $< -o $@

%.o: %.cc %.h
	$(CC) $(OCC_OPT) -c $< -o $@

%.o: %.cc
	$(CC) $(OCC_OPT) -c $< -o $@

